/*
 * Copyright Owen Watson 2009
 * 
 * This file is part of ArtNotes.
 *
 * ArtNotes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * ArtNotes is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with ArtNotes.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.CoreGraphics;
using MonoTouch.CoreAnimation;

namespace ArtNotes
{
	/// <summary>
	/// This is not used in the current release,
	/// however the idea is to let people do more
	/// advanced stuff, such as editing tag names
	/// etc.
	/// </summary>
	public class ANAdvancedTabController : UIViewController
	{
		
		public ANDatabase anDB;
		UIBarButtonItem btnSave;

		public ANAdvancedTabController ()
		{
		}
		
		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			View.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("Images/notepaper.png"));
			View.SizeToFit ();
			anDB = AN_APP_OPTIONS.AN_DATABASE;
			
			btnSave = new UIBarButtonItem();
			btnSave.Title = "Save";
			btnSave.Clicked += delegate {
				this.saveChanges();
			};
			
			this.NavigationItem.SetRightBarButtonItem(btnSave,false);
		}
		
		UITextField[] tagNames;
		
		public override void ViewDidAppear (bool animated)
		{
			Tag[] tags = anDB.ListAllTags ().ToArray ();
			
			tagNames = new UITextField[tags.Length];
			
			for (int i = 0; i < tags.Length; i++)
			{
				tagNames[i] = createInputItem(new PointF(20, (i+1) * 30));
				tagNames[i].Text = tags[i].TagName;
				this.View.AddSubview(tagNames[i]);
			}
		}

		private UITextField createInputItem(PointF location)
		{
			UITextField tmp = new UITextField(new RectangleF (location,new SizeF(180f,25f)));
			tmp.BorderStyle = UITextBorderStyle.RoundedRect;
			tmp.BackgroundColor = UIColor.White;
			tmp.Font = UIFont.FromName ("Marker Felt", 18);
			tmp.TextColor = UIColor.DarkGray;
			tmp.ShouldReturn = editFieldInputHandler;
			return tmp;
		}

		bool editFieldInputHandler(UITextField tf)
		{
			tf.ResignFirstResponder();
			return true;
		}
		
		public void saveChanges()
		{
			Console.WriteLine("Save Button Pressed");
		}
	}
}
